package com.zh.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.Result;
import com.zh.dto.DishDto;
import com.zh.dto.SetmealDto;
import com.zh.entity.Dish;
import com.zh.entity.Setmeal;
import com.zh.entity.SetmealDish;
import com.zh.service.CategoryService;
import com.zh.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 套餐 前端控制器
 * </p>
 *
 * @author 你爹
 * @since 2023-09-21
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public Result save(@RequestBody SetmealDto setmealDto){
        log.info("保存setmealDto={}",setmealDto);
        setmealService.saveWithDishes(setmealDto);
        return Result.success();
    }

    @GetMapping("/page")
    public Result<Page<SetmealDto>> page(Integer page,Integer pageSize,String name){
        log.info("page={},pageSize={},name={}",page,pageSize,name);
        Page<Setmeal> p =new Page<>(page,pageSize);
        Page<SetmealDto> pageInfo= new Page<>(page,pageSize);
        //先查setmeal基本信息
        LambdaQueryWrapper<Setmeal> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(name!=null,Setmeal::getName,name);
        wrapper.orderByAsc(Setmeal::getUpdateTime);
        setmealService.page(p,wrapper);
        BeanUtils.copyProperties(p,pageInfo,"records");
        List<SetmealDto> setmealDtoList = p.getRecords().stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            //获取套餐类型
            setmealDto.setCategoryName(categoryService.getById(setmeal.getCategoryId()).getName());
            return setmealDto;
        }).collect(Collectors.toList());
        pageInfo.setRecords(setmealDtoList);
        return Result.success(pageInfo);
    }

    /**
     * 根据分类id/ 套餐名称查setmeal
     * @param
     * @return
     */
    @GetMapping("/list")
    @Cacheable(cacheNames = "setmeal",key ="'setmeal'+'_'+#setmeal.categoryId+'_'+#setmeal.status")
    public Result<List<Setmeal>> list(Setmeal setmeal){
        log.info("查询分类id为{}/name={}setmeal",setmeal.getCategoryId(),setmeal.getName());
        LambdaQueryWrapper<Setmeal> wrapper= new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmealList = setmealService.list(wrapper);
        return Result.success(setmealList);
    }

    @PostMapping("/status/{status}")
    public Result status(@PathVariable Integer status,@RequestParam("ids") List<Long> ids){
        log.info("status={},ids={}",status,ids);
        setmealService.updateStatus(status,ids);
        return Result.success();
    }
    @DeleteMapping
    public Result delete(@RequestParam("ids") List<Long> ids){
        log.info("ids:{}",ids);
        setmealService.deleteWithDishes(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<SetmealDto> getById(@PathVariable Long id){
        log.info("id;{}",id);
        SetmealDto setmealDto = setmealService.getSetmealById(id);
        log.info("查询到的setmealDto:{}",setmealDto);
        return Result.success(setmealDto);
    }

    @GetMapping("/dish/{id}")
    public Result<List<DishDto>> getOne(@PathVariable Long id){
        log.info("setmealId:{}",id);
        List<DishDto> dishDtoList= setmealService.getOneSetmeal(id);
        return Result.success(dishDtoList);
    }
    @PutMapping
    public Result update(@RequestBody SetmealDto setmealDto){
        log.info("setmealDto:{}",setmealDto);
        setmealService.updateWithDishes(setmealDto);
        return Result.success();
    }


}

